package file;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

//@Controller
//@ResponseBody
@RestController
public class ContinueFileUp {
    @GetMapping("/getSize")
    public long getSize(String fileName, HttpSession session){
        String dir=new SimpleDateFormat("yyyyMMdd").format(new Date());
        String path=session.getServletContext().getRealPath("/file/"+dir+"/"+fileName);
        System.out.println("获取文件大小...");
        File f=new File(path);
        return f.exists()?f.length():0;
    }

    @PostMapping("/upload2")
    void upload(MultipartFile f,Long start,String filename,HttpSession session) throws Exception {
        String dir=new SimpleDateFormat("yyyyMMdd").format(new Date());
        String path=session.getServletContext().getRealPath("/file/"+dir);
        System.out.println(path);
        File file=new File(path);
        file.mkdirs();//创建对应的子目录
        System.out.println("文件名："+filename+",起始位置："+start);
        file=new File(file,filename);
        FileOutputStream fos=new FileOutputStream(file,true);
        fos.write(f.getBytes());
        fos.close();
    }

}
